#include <stdio.h>
#include <stdlib.h>
#include "terrain.h"



const char terrain1[20][21] = {
    "XXXXXXXXXXXXXXXXXXXX",
    "X..................X",
    "X..................X",
    "X..................X",
    "X..................X",
    "X---------------...X",
    "X..............-...X",
    "X..............-...X",
    "X..............-...X",
    "X..............-...X",
    "X..-------------...X",
    "X..-...............X",
    "X..-...............X",
    "X..-...............X",
    "X..-...............X",
    "X..-...............X",
    "X..-------------&&&&",
    "X...............&&&&",
    "X...............&&&&",
    "XXXXXXXXXXXXXXXX&&&&"};




Terrain* terrainInit()
{
    int x,y;

    Terrain *pTerrain;

    pTerrain = (Terrain*)malloc(sizeof(Terrain));

    pTerrain->dimx=20;
    pTerrain->dimy=20;

    for(x=0;x<(pTerrain->dimx);x++)
        for(y=0;y<(pTerrain->dimy);y++)
            pTerrain->ter[x][y] = terrain1[x][y];

    return(pTerrain);

}


void TerrainLibere(Terrain *pTerrain)
{
     free (pTerrain);
}

int getDimX(const Terrain *pTerrain)
{
     return pTerrain->dimx;
}

int getDimY(const Terrain *pTerrain)
{
      return pTerrain->dimy;
}

Terrain* nettoyerTerrain(Terrain *pTerrain)
{
    Terrain *terrain;

    TerrainLibere(pTerrain);
    terrain=terrainInit();

    return(terrain);
}


void majTerrain(Terrain *pTerrain, ListeCreatures *listecreatures, ListeTours *listetours)
{
    int nbCreatures, nbTours, i;
    int positionx, positiony,type;
    Creature *creatureTemp;
    Tour *tourTemp;

    nbCreatures=getNbCreatures(listecreatures);
    nbTours=getNbTours(listetours);

    pTerrain=nettoyerTerrain(pTerrain);

    for(i=0;i<nbCreatures;i++)
    {
        creatureTemp=getCreature(listecreatures, i);
        positionx=getCreaPositionx(creatureTemp);
        positiony=getCreaPositiony(creatureTemp);
        type=getCreaType(creatureTemp);
        switch(type)
        {
            case 1: pTerrain->ter[positiony][positionx]='C';
            break;
            case 2: pTerrain->ter[positiony][positionx]='D';
            break;
            case 3: pTerrain->ter[positiony][positionx]='E';
            break;
            case 4: pTerrain->ter[positiony][positionx]='F';
            break;
            case 5: pTerrain->ter[positiony][positionx]='G';
            break;
        }

    }

    for(i=0;i<nbTours;i++)
    {
        tourTemp=getTour(listetours,i);
        positionx=getTourPositionx(tourTemp);
        positiony=getTourPositiony(tourTemp);
        pTerrain->ter[positiony][positionx]='T';
    }

}

int terrainAvancable(Terrain *pTerrain, int positionx, int positiony)
{
    if((pTerrain->ter[positiony][positionx]=='-') || (pTerrain->ter[positiony][positionx] =='&'))
        return(1);
    else
        return(0);
}

int terrainConstructible(Terrain *pTerrain, int positionx, int positiony)
{
    if(pTerrain->ter[positiony][positionx]=='.')
     return(1);
    else
        return(0);
}





